package com.realestate.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.realestate.constants.RecordStatus;
import com.realestate.model.BrtbProduct;
import com.realestate.model.QBrtbCategory;
import com.realestate.model.QBrtbProduct;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class ProductQueryDaoImpl extends GenericDAOImpl<BrtbProduct, Long> implements ProductQueryDao {

	private static final Logger logger = Logger.getLogger(ProductQueryDaoImpl.class);
	
	@Override
	public List<BrtbProduct> getListproductByCategoryCode(String categoryCode, Long limit) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbCategory category = QBrtbCategory.brtbCategory;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(category.categoryCode.eq(categoryCode))
			 .and(category.recordStatus.eq(RecordStatus.O))
			 .and(product.recordStatus.eq(RecordStatus.O));
		
		query.from(product).leftJoin(product.brtbCategory, category).where(where).orderBy(product.makeDate.desc()).limit(limit);
		
		List<BrtbProduct> products = query.list(product);
		return products;
	}
}
